package cn.itsource.article.controller;


import cn.itsource.article.domain.Article;
import cn.itsource.article.query.ArticleQuery;
import cn.itsource.article.service.ArticleService;
import cn.itsource.param.domain.Like;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 新增或修改
     * @param a
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Article a){
        if (a.getId()==null){
            articleService.insert(a);
            return AjaxResult.ok();
        }else {
            articleService.updateByPrimaryKeySelective(a);
            return AjaxResult.ok();
        }
    }


    /**
     * 门户网站分类
     * @return
     */
    @PatchMapping("/group")
    public AjaxResult group(){
        return AjaxResult.ok(articleService.group());
    }

    /**
     * 归档分组
     * @return
     */
    @PatchMapping("/placeOnFile")
    public AjaxResult placeOnFile(){
        return AjaxResult.ok(articleService.placeOnFile());
    }



    /**
     * 分页查询
     * @param articleTypeQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody ArticleQuery articleTypeQuery){
        try {
            //System.out.println(articleTypeQuery);
            return AjaxResult.ok(articleService.pageInfo(articleTypeQuery));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 批量删除
     * @param a
     * @return
     */
    @PatchMapping("/batchDeletion")
    public AjaxResult batchDeletion(@RequestBody List<Article> a){

        try {
            articleService.deleteAll(a);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            articleService.deleteByPrimaryKey(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @GetMapping ("/{articleId}")
        public AjaxResult get(@PathVariable("articleId") Long articleId){
            try {
                Article article = articleService.selectId(articleId);
                return AjaxResult.ok(article);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


    /**
     * 查询相关文章
     * @param articleId
     * @return
     */
    @PostMapping("/aboutArticles/{articleId}")
    public AjaxResult aboutArticles(@PathVariable("articleId") Long articleId){
        try {
            List<Article> articles = articleService.aboutArticles(articleId);
            return AjaxResult.ok(articles);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 点赞量
     * @param like
     * @return
     */
    @PostMapping("/like")
    public AjaxResult like(@RequestBody Like like){
        articleService.like(like);
        return AjaxResult.ok();
    }

    /**
     * 刷新量
     * @param articleId
     * @return
     */

    @PutMapping("/yueDu/{articleId}")
    public AjaxResult incrementReadCount(@PathVariable("articleId") Long articleId) {
        articleService.incrementReadCount(articleId);
        return AjaxResult.ok();
    }


    /**
     * 柱状图
     * @param
     * @return
     */
    @GetMapping("/echarts")
    public AjaxResult drawColumnChart() {
        List<Article> namelist = articleService.drawColumnChart();
        return AjaxResult.ok(namelist);
    }


}
